<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SSH 实时脚本执行 - Go 版本</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }
        #output {
            background-color: #1e1e1e;
            color: #fff;
            border: 1px solid #333;
            padding: 10px;
            height: 400px;
            overflow-y: auto;
            font-family: monospace;
            margin: 20px 0;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 100px;
            margin-right: 10px;
        }
        input {
            padding: 5px;
            width: 300px;
        }
        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }
        button:disabled {
            background-color: #cccccc;
        }
        .error {
            color: #ff6b6b;
        }
        .success {
            color: #4CAF50;
        }
    </style>
</head>
<body>
    <h1>SSH 实时脚本执行 - Go 版本</h1>
    <form id="sshForm">
        <div class="form-group">
            <label for="host">服务器地址：</label>
            <input type="text" id="host" name="host" required value="192.168.1.6">
        </div>
        
        <div class="form-group">
            <label for="username">用户名：</label>
            <input type="text" id="username" name="username" required value="dev">
        </div>
        
        <div class="form-group">
            <label for="password">密码：</label>
            <input type="password" id="password" name="password" required value="123456">
        </div>
        
        <div class="form-group">
            <label for="script">脚本路径：</label>
            <input type="text" id="script" name="script" required value="/www/wwwroot/script.py">
        </div>
        
        <button type="button" onclick="executeSSH()">执行脚本</button>
    </form>
    
    <h2>执行输出：</h2>
    <div id="output"></div>

    <script>
        let ws;
        
        function executeSSH() {
            const formData = new FormData(document.getElementById('sshForm'));
            const outputDiv = document.getElementById('output');
            outputDiv.innerHTML = '';

            const submitButton = document.querySelector('button');
            submitButton.disabled = true;

            if (ws) {
                ws.close();
            }
            
            ws = new WebSocket('ws://localhost:2345/ws');
            
            ws.onopen = function() {
                const params = {};
                for (const pair of formData) {
                    params[pair[0]] = pair[1];
                }
                ws.send(JSON.stringify(params));
            };
            
            ws.onmessage = function(event) {
                const message = JSON.parse(event.data);
                const line = document.createElement('div');
                line.textContent = message.data;
                line.className = message.type === 'error' ? 'error' : 'success';
                outputDiv.appendChild(line);
                outputDiv.scrollTop = outputDiv.scrollHeight;
            };
            
            ws.onclose = function() {
                const line = document.createElement('div');
                line.textContent = '连接已结束';
                outputDiv.appendChild(line);
                submitButton.disabled = false;
            };
            
            ws.onerror = function(error) {
                const line = document.createElement('div');
                line.textContent = 'WebSocket 错误';
                line.className = 'error';
                outputDiv.appendChild(line);
                submitButton.disabled = false;
            };
        }
    </script>
</body>
</html> 